package com.scud.system.controller;

import com.scud.common.result.Result;
import com.scud.model.PageResult;
import com.scud.model.system.dto.AssignRoleDto;
import com.scud.model.system.dto.PasswordDto;
import com.scud.model.system.dto.UserPageDto;
import com.scud.model.system.pojo.User;
import com.scud.system.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

/**
 * @author vermouth
 */
@RestController
@RequestMapping("/api/system/user")
@Slf4j
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 修改用户信息
     *
     * @param user:
     * @return Result
     * @author vermouth
     */
    @PutMapping("updateUserInfo")
    public Result updateUserInfo(@RequestBody User user) {
        log.info("修改用户信息:{}", user);
        userService.updateUserInfo(user);
        return Result.success();
    }

    /**
     * 设置用户密码
     *
     * @param dto:
     * @return Result
     * @author vermouth
     */
    @PutMapping("/setPassword")
    public Result setPassword(@RequestBody PasswordDto dto) {
        log.info("设置用户密码:{}", dto);
        userService.setPassword(dto);
        return Result.success();
    }

    /**
     * 用户条件分页查询
     *
     * @param page
     * @param limit
     * @param userPageDto
     * @return
     */
    @PostMapping("/page/{page}/{limit}")
    @PreAuthorize("hasAuthority('USER_LIST')")
    public Result<PageResult<User>> page(@PathVariable Integer page,
                                         @PathVariable Integer limit,
                                         @RequestBody UserPageDto userPageDto) {
        log.info("用户条件分页查询:{},{},{}", page, limit, userPageDto);
        PageResult<User> res = userService.pageList(page, limit, userPageDto);
        return Result.success(res);
    }

    /**
     * 新增用户
     *
     * @param user
     * @return
     */
    @PostMapping("/save")
    @PreAuthorize("hasAuthority('USER_ADD')")
    public Result save(@RequestBody User user) {
        log.info("新增用户:{}", user);
        userService.addUser(user);
        return Result.success();
    }

    /**
     * 更改用户
     *
     * @param user
     * @return
     */
    @PutMapping("/update")
    @PreAuthorize("hasAuthority('USER_UPDATE')")
    public Result update(@RequestBody User user) {
        log.info("修改用户:{}", user);
        userService.update(user);
        return Result.success();
    }

    /**
     * 删除用户
     *
     * @param id
     * @return
     */
    @DeleteMapping("/delete/{id}")
    @PreAuthorize("hasAuthority('USER_DELETE')")
    public Result delete(@PathVariable Long id) {
        log.info("删除用户:{}", id);
        userService.deleteById(id);
        return Result.success();
    }

    /**
     * 为用户分配角色
     *
     * @param assignDto
     * @return
     */
    @PostMapping("/assign")
    @PreAuthorize("hasAuthority('USER_ASSIGN_ROLE')")
    public Result assign(@RequestBody AssignRoleDto assignDto) {
        log.info("为用户分配角色:{}", assignDto);
        userService.assign(assignDto);
        return Result.success();
    }
}
